**************************************************************************************************************************************************
****************************** Wealth-Income Ratios in Free Market Capitalism: Switzerland, 1900-2020 ********************************************
**************************************************************************************************************************************************
clear all
set more off
set scheme s1color  
cap set scheme mygraphs

**Author: Enea Baselgia and Isabel Z. Martinez
**Date: January, 25 2023

*** set this path to the current directory
*** global mypath "C:\Users\EBaselgia\Dropbox\WIR_project\Publication_process\replication"


* PLOT TAX BURDEN BY SOURCE AND ACROSS COUNTRIES IN % OF GDP

***parademo 
cd "$mypath/"
do "6_pareademo.do"



 * * * * LOAD DATA ON DIFFERENT TAXES FROM THE OECD * * * *
cd "$mypath/raw_data/tax_revenue_OECD/"

import delim using "Tax_revenue.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxrev
cap rename ïlocation location
drop indicator
tempfile data1
save "`data1'"

import delim using "Tax_on_personal_income.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxincome
cap rename ïlocation location
drop indicator
tempfile data2
save "`data2'"

import delim using "SS_contributions.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxss
cap rename ïlocation location
drop indicator
tempfile data3
save "`data3'"

import delim using "Tax_on_payroll.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxparoll
cap rename ïlocation location
drop indicator
tempfile data4
save "`data4'"

import delim using "Tax_on_corporate_profits.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxcorp
cap rename ïlocation location
drop indicator
tempfile data5
save "`data5'"

import delim using "Tax_on_net_wealth.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxproperty
cap rename ïlocation location
drop indicator
tempfile data6
save "`data6'"

import delim using "Tax_on_goods+services.csv", clear
keep if measure== "PC_GDP"
drop frequency flagcodes subject measure
rename value taxgoodserv
cap rename ïlocation location
drop indicator
tempfile data7
save "`data7'"

* combine all the data
use  "`data1'"

forval n=2/7 {
	merge 1:1 time location using "`data`n''", gen(merge_`n')
}
rename location country
rename time year

keep if (country == "CHE" | country == "DEU" | country == "ESP" | country == "FRA" | country == "GBR" | country == "SWE" | country == "USA" | country == "OAVG")

replace country = "OECD" if country == "OAVG"


* * * * PREPARE DATA FOR PLOTTING * * * * 
* 1) make sure they sum up more or less nicely
gen sumdiff = taxrev - taxincome - taxss- taxparoll - taxcorp - taxproperty - taxgoodserv
sum sumdiff, de

*2) gen shares and sums to make area plots
foreach var in income  ss paroll  corp  property  goodserv {
	gen sh_`var' = tax`var' / taxrev
}

gen area1 = sh_income
gen area2 = area1 + sh_ss
gen area3 = area2 + sh_paroll
gen area4 = area3 + sh_goodserv
gen area5 = area4 + sh_property
gen area6 = area5 + sh_corp

gen var1 = taxincome
gen var2 = var1 + taxss
gen var3 = var2 + taxparoll
gen var4 = var3 + taxgoodserv
gen var5 = var4 + taxproperty
gen var6 = var5 + taxcorp

sort year country


* * * * GENERATE PLOTS OF TAX BURDEN BY COUNTRY * * * *   
cd "$mypath/output/figures/"


foreach mycountry in  CHE DEU ESP FRA GBR SWE USA OECD {
	preserve
		keep if country == "`mycountry'"

		local myarea
		local myarea `myarea' (parea var6 year, color(black) pattern(pattern1)) 
		local myarea `myarea' (parea var5 year, color(gs7) pattern(pattern6)) 
		local myarea `myarea' (area var4 year, color(gs15) lcol(gs12)) 
		local myarea `myarea' (area var3 year, color(gs11)) 
		local myarea `myarea' (area var2 year, color(gs7))
		local myarea `myarea' (area var1 year, color(black)) 
		
		tw `myarea' , 																///
			legend(order(4 "payroll" 5 "social security" 6 "income"  				///
			1 "corporate income"  2 "property" 3 "goods and services") colfirst) 	///
			xtitle("") ytitle("Total tax revenue in % of GDP") 	 					///
			xlab(1965(10)2020, grid) xmtick(1970(10)2020, grid) ylab(0(10)50, grid ang(h))						///
			name(gr_`mycountry', replace) 
			
	graph export "Fig_append_B_4`mycountry'.pdf", replace
	restore
}


* COMBINE ALL COUNTRIES IN TO ONE GRAPH *

foreach mycountry in  CHE DEU ESP FRA GBR SWE USA OECD {
	preserve
		keep if country == "`mycountry'"
		
	if "`mycountry'" == "CHE" {
		local mytitle "Switzerland"
	}
	if "`mycountry'" == "DEU" {
		local mytitle "Germany"
	}
	if "`mycountry'" == "ESP" {
		local mytitle "Spain"
	}
	if "`mycountry'" == "FRA" {
		local mytitle "France"
	}
	if "`mycountry'" == "GBR" {
		local mytitle "Great Britain"
	}
	if "`mycountry'" == "SWE" {
		local mytitle "Sweden"
	}
	if "`mycountry'" == "USA" {
		local mytitle "United States"
	}
	if "`mycountry'" == "OECD" {
		local mytitle "OECD"
	}

		local myarea
		local myarea `myarea' (parea var6 year, color(black) pattern(pattern1)) 
		local myarea `myarea' (parea var5 year, color(gs7) pattern(pattern6)) 
		local myarea `myarea' (area var4 year, color(gs15) lcol(gs12)) 
		local myarea `myarea' (area var3 year, color(gs11)) 
		local myarea `myarea' (area var2 year, color(gs7))
		local myarea `myarea' (area var1 year, color(black)) 
		
		tw `myarea' , 															///
			legend(order(5 "social security" 6 "income"  3 "goods and services" ///
			4 "payroll" 1 "corporate income"  2 "property") 					///
			colfirst col(3) size(*0.6) span) title(`mytitle', size(*0.6)) 		///
			xtitle("") ytitle("Total tax revenue in % of GDP", size(*0.7)) 	 	///
			xlab(1965(10)2020, grid ang(45) labsize(*0.7)) xmtick(1970(10)2020, grid ang(45) labsize(*0.7))			///
			ylab(0(10)50, grid ang(h) labsize(*0.7))							///
			name(gr_`mycountry', replace) 
	restore
}

/*
grc1leg2 gr_CHE gr_USA gr_ESP gr_GBR, span
gr_edit style.editstyle declared_ysize(17) editcopy
gr_edit style.editstyle declared_xsize(19) editcopy
gr_edit legend.Edit , style(key_xsize(8)) keepstyles 
graph export "RR_tax_revenue_CHE_USA_ESP_GBR.pdf", replace

grc1leg2 gr_OECD gr_DEU gr_FRA gr_SWE, span
gr_edit style.editstyle declared_ysize(17) editcopy
gr_edit style.editstyle declared_xsize(19) editcopy
gr_edit legend.Edit , style(key_xsize(8)) keepstyles 
graph export "RR_tax_revenue_OECD_DEU_FRA_SWE.pdf", replace
*/

grc1leg2 gr_CHE gr_USA gr_ESP gr_GBR gr_DEU gr_FRA gr_SWE gr_OECD, span cols(2) imargin(tiny)
gr_edit style.editstyle declared_ysize(20) editcopy
gr_edit style.editstyle declared_xsize(12) editcopy
gr_edit legend.Edit , style(key_xsize(8)) keepstyles 
graph export "Fig_append_B_5.pdf", replace


bys country: sum var6
bys country: sum taxrev

sort country year

* TOTAL TAX BURDEN IN INTERNATIONAL COMPARISON *

gr tw   (line taxrev year if country == "CHE", lcol(gs0) lw(*2.8))  						///
                (line taxrev year if country == "USA", lcol(gs14))  						///
                (line taxrev year if country == "ESP", lcol(gs0) lpattern(dot) lw(*1.5))	///
                (line taxrev year if country == "GBR", lcol(gs6) lpattern(- - -))			///
                (line taxrev year if country == "DEU", lcol(gs5))  							///
                (line taxrev year if country == "FRA", lcol(gs11))  						///
                (line taxrev year if country == "SWE", lcol(gs14) lw(*2.2))  				///
                (line taxrev year if country == "OECD", lcol(gs7) lw(*2.2) lpat(longdash))	///
		, legend(order(1 "CHE" 2 "USA" 3 "ESP" 4 "GBR" 7 "SWE"  6 "FRA"  					///
						5 "DEU" 8 "OECD") col(4) colfirst) 									///
		xtitle("") ytitle("Total tax revenue in % of GDP")      							///
		xlab(1965(10)2020, grid) xmtick(1970(10)2020, grid) ylab(0(10)50, grid ang(h))     
graph export "Fig_append_B_4`mycountry'.pdf", replace
 
//Fig_append_B_3

